package Engine.UIControl
{
	import Interface.UIControl.IUIBase;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import library.Function.GlobalFun;
	
	public class UIBase implements IUIBase
	{
		private static var disAbleFilters:Array = new Array();
		
		protected var _normalFilters:Array = null;
		
		private var _isMouseDown:Boolean;
		private var _isMouseIn:Boolean;
		protected var _content:Sprite;
		
		public function UIBase(p_content:Sprite = null,p_width:Number = 0.0,p_height:Number = 0.0)
		{
			super();
			if(disAbleFilters.length == 0)
			{
				disAbleFilters.push(GlobalFun.getDisColorFilter());
			}
			
			
			
			if(p_content == null)
			{
				p_content = new Sprite;
				p_content.graphics.lineStyle();
				p_content.graphics.beginFill(0x000000,0.0);
				p_content.graphics.drawRect(0, 0, p_width,p_height);
				p_content.graphics.endFill();
			}
			
			this._content = p_content;
				
			this.initControl();
			this.regesterEvent();
			
		}
		
		private function initControl():void
		{
			this._isMouseDown = false;
			this._isMouseIn = false;
		}
		
		private function _onMouseDown(event:MouseEvent):void
		{
			if(this._isMouseDown)
				return;
			this._isMouseDown = true;
			this._content.stage.addEventListener(MouseEvent.MOUSE_UP,_onMouseUp);
			onMouseDown(event);
		}
		
		private function _onMouseUp(event:MouseEvent):void
		{
			this._isMouseDown = false;
			this._content.stage.removeEventListener(MouseEvent.MOUSE_UP,_onMouseUp);
			onMouseUp(event);
			if(this._isMouseIn)
			{
				onMouseClick(event);
			}
		}
		
		
		protected function _onMouseIn(event:MouseEvent):void
		{
			if(this._isMouseIn)
				return;
			this._isMouseIn = true;
			onMouseIn(event);
		}
		
		private function _onMouseOut(event:MouseEvent):void
		{
			onMouseOut(event);
			this._isMouseIn = false;
		}
		
		public function regesterEvent():void
		{
			this._content.addEventListener(MouseEvent.MOUSE_DOWN,_onMouseDown);
			this._content.addEventListener(MouseEvent.MOUSE_MOVE,_onMouseIn);
			this._content.addEventListener(MouseEvent.MOUSE_OUT,_onMouseOut);
		}
		
		public function removeEvent():void
		{
			this._content.removeEventListener(MouseEvent.MOUSE_DOWN,_onMouseDown);
			this._content.removeEventListener(MouseEvent.MOUSE_MOVE,_onMouseIn);
			this._content.removeEventListener(MouseEvent.MOUSE_OUT,_onMouseOut);
		}
		
		/*
		公共接口
		*/
		public function setDisable():void
		{
			this._content.mouseEnabled = false;
			this._content.filters = disAbleFilters;
			this._normalFilters = disAbleFilters;
		}
		
		public function setAble():void
		{
			this._content.mouseEnabled = true;
			this._content.filters = null;
			this._normalFilters = null;
		}
		
		public function show(parent:DisplayObjectContainer):void
		{
			parent.addChild(this._content);
		}
		
		public function get content():Sprite
		{
			return this._content;
		}
		
		public function get x():Number
		{
			return this._content.x;
		}
		public function set x(n:Number):void
		{
			this._content.x = n;
		}
		public function get y():Number
		{
			return this._content.y;
		}
		public function set y(n:Number):void
		{
			this._content.y = n;
		}
		public function get width():Number
		{
			return this._content.width;
		}
		public function set width(n:Number):void
		{
			this._content.width = n;
		}
		public function get height():Number
		{
			return this._content.height;
		}
		public function set height(n:Number):void
		{
			this._content.height = n;
		}
		
		/*
		重载接口
		*/
		protected function onMouseClick(event:MouseEvent):void
		{
			
		}
		
		protected function onMouseDown(event:MouseEvent):void
		{
			
		}
		
		protected function onMouseUp(event:MouseEvent):void
		{
			
		}
		
		protected function onMouseIn(event:MouseEvent):void
		{
			
		}
		
		protected function onMouseOut(event:MouseEvent):void
		{
			
		}
		
	}
}